package com.starbucks.opencase.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.entity.entUserInfo;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.function.funTest;
import com.starbucks.opencase.request.reqData;
import com.starbucks.opencase.tool.toolAdminAccount;
import com.starbucks.opencase.tool.toolUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/admin/player")
public class conAdminPlayer{
    @Autowired
    private funTest funTest;
    @Autowired
    private funSession funSession;
    @Autowired
    private toolUserInfo toolUserInfo;
    @Autowired
    private toolAdminAccount toolAdminAccount;
    @PostMapping("/query/online")
    public String queryOnline(@RequestBody reqData info) {
        try {
            ObjectMapper om = new ObjectMapper();
            JsonNode me = om.readTree(info.getData());
            ObjectNode on=om.createObjectNode();
            on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
            on.put(enumCMKey.status.name(), false);
            if(!funTest.hasPhone(me,on))return on.toString();
            if(!funTest.hasCommand(me,on))return on.toString();
            String phone= funTest.getPhone(me);
            String command= funTest.getCommand(me);
            if(!toolAdminAccount.queryCommand(phone).equals(command)){
                on.put(enumCMKey.tip.name(), enumCMTip.error_command.getTip());
                return on.toString();
            }
            on.put(enumCMKey.status.name(), true);
            on.put(enumCMKey.online.name(), funSession.getOnlineNum());
            return on.toString();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/query/list")
    public String queryList(@RequestBody reqData info){
        try{
            ObjectMapper om = new ObjectMapper();
            JsonNode me = om.readTree(info.getData());
            ObjectNode on=om.createObjectNode();
            on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
            on.put(enumCMKey.status.name(), false);
            if(!funTest.hasPhone(me,on))return on.toString();
            if(!funTest.hasCommand(me,on))return on.toString();
            String phone= funTest.getPhone(me);
            String command= funTest.getCommand(me);
            if(!toolAdminAccount.queryCommand(phone).equals(command)){
                on.put(enumCMKey.tip.name(), enumCMTip.error_command.getTip());
                return on.toString();
            }
            if(!funTest.hasNowPageNum(me,on))return on.toString();
            if(!funTest.hasNickname(me,on))return on.toString();
            String nickname=funTest.getNickname(me);
            int num= funTest.getNowPageNum(me);
            List<entUserInfo> da=toolUserInfo.queryList(nickname,num);
            int all= toolUserInfo.queryAllPageNum(nickname);
            on.put(enumCMKey.allPageNum.name(), all);
            on.put(enumCMKey.nowPageNum.name(), num);
            ArrayNode an=om.createArrayNode();
            for(entUserInfo t:da){
                ObjectNode onn=om.createObjectNode();
                onn.put(enumCMKey.uid.name(), t.getUid());
                onn.put(enumCMKey.nickname.name(), t.getNickname());
                onn.put(enumCMKey.gender.name(), t.getGender());
                an.add(onn);
            }
            on.put(enumCMKey.data.name(),an);
            on.put(enumCMKey.status.name(), true);
            return on.toString();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
